home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland Pascal with Objects 7.0 / CHESS.ZIP / OWLCMDLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.5 KB  |  232 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Chess Demo                     }
  4. {   COMMDLG wrappers                             }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit OWLCmDlg;
  10.  
  11. interface
  12.  
  13. {$R OWLCMDLG.RES}
  14.  
  15. uses WinProcs, WinTypes, OWindows, ODialogs, CommDlg, WinDos, Strings;
  16.  
  17. type
  18.  
  19.    { TCDFileDlg builds an OWL object around a Windows 3.1 Common Dialog.
  20.      By using the OWL object's Instance function pointer as the
  21.      common dialog's hook procedure, the OWL object will get messages
  22.      just as it would for a normal dialog (for the most part).
  23.  
  24.      Descendents of TCDFileDlg implement specific types of file dialogs:
  25.      File Open, File Save, File Save As, and special purpose dialogs.}
  26.  
  27.    PCDFileDlg = ^TCDFileDlg;
  28.    TCDFileDlg = object(TDialog)
  29.      OFN : TOpenFileName;
  30.      constructor Init(AParent: PWindowsObject; AFlags: Longint;
  31.        AFileName: PChar; ANameLength: Word; AFilter: PChar);
  32.      destructor Done;  virtual;
  33.      function Create: Boolean; virtual;
  34.      function Execute: Integer; virtual;
  35.      function CDExecute: Bool; virtual;
  36.      procedure OK(var Msg: TMessage);
  37.        virtual id_First + id_OK;
  38.      procedure Cancel(var Msg: TMessage);
  39.        virtual id_First + id_Cancel;
  40.    end;
  41.  
  42.    { TCDFileOpen implements a File Open common dialog.  If the main program
  43.      is using BWCC, then this object makes the common dialog use a BWCC
  44.      dialog template.  }
  45.  
  46.    PCDFileOpen = ^TCDFileOpen;
  47.    TCDFileOpen = object(TCDFileDlg)
  48.      constructor Init(AParent: PWindowsObject; AFlags: Longint;
  49.        AFileName: PChar; ANameLength: Word; AFilter: PChar);
  50.    end;
  51.  
  52.    PCDFileSaveAs = ^TCDFileSaveAs;
  53.    TCDFileSaveAs = object(TCDFileOpen)
  54.      constructor Init(AParent: PWindowsObject; AFlags: Longint;
  55.        AFileName: PChar; ANameLength: Word; AFilter: PChar);
  56.      function CDExecute: Bool; virtual;
  57.    end;
  58.  
  59.  
  60. implementation
  61.  
  62. const
  63.   dlgCDFileOpen_BWCC   = MakeIntResource(32520);
  64.  
  65. constructor TCDFileDlg.Init(AParent: PWindowsObject; AFlags: Longint;
  66.   AFileName: PChar; ANameLength: Word; AFilter: PChar);
  67. var
  68.   TempName: array[0..fsFileName] of Char;
  69.   TempExt : array[0..fsExtension] of Char;
  70.  
  71. begin
  72.    TDialog.Init(AParent,nil);
  73.    FillChar(OFN,Sizeof(OFN),0);
  74.    with OFN do
  75.    begin
  76.      lStructSize := SizeOf(OFN);
  77.      hwndOwner := AParent^.hWindow;
  78.      @lpfnHook := Instance;
  79.      Flags     := AFlags or OFN_ENABLEHOOK;
  80.      hInstance := System.hInstance;
  81.      lpstrFilter := AFilter;
  82.      lpstrFileTitle  := nil;
  83.      nMaxFileTitle   := 0 ;
  84.      GetMem(lpstrInitialDir,Succ(fsDirectory));
  85.      lpstrFile := AFileName;
  86.      nMaxFile  := ANameLength;
  87.      FileExpand(lpstrFile,AFileName);
  88.      FileSplit(lpstrFile,lpstrInitialDir,TempName,TempExt);
  89.      StrCat(StrCopy(lpstrFile,TempName),TempExt);
  90.    end;
  91. end;
  92.  
  93.  
  94. destructor TCDFileDlg.Done;
  95. begin
  96.   FreeMem(OFN.lpstrInitialDir,Succ(fsDirectory));
  97.   TDialog.Done;
  98. end;
  99.  
  100. function    TCDFileDlg.Create: Boolean;
  101. begin
  102.   Create := False;  { Cannot create a non-modal File Open dialog }
  103. end;
  104.  
  105. { Basically, This is the code from TDialog.Execute with the call to
  106.   DialogBoxParam changed to CDExecute }
  107. function    TCDFileDlg.Execute: Integer;
  108. var
  109.   CDError: Longint;
  110.   OldKbHandler: PWindowsObject;
  111. begin
  112.   if Status = 0 then
  113.   begin
  114.     DisableAutoCreate;
  115.     EnableKBHandler;
  116.     IsModal := True;
  117.     OldKbHandler := Application^.KBHandlerWnd;
  118.     if CDExecute then
  119.       Execute := id_ok
  120.     else
  121.     begin
  122.       CDError := CommDlgExtendedError;
  123.       if CDError = 0 then
  124.         Execute := id_Cancel
  125.       else
  126.       begin
  127.         Status := -CdError;
  128.         Execute := Status;
  129.       end;
  130.     end;
  131.     Application^.KBHandlerWnd := OldKbHandler;
  132.     HWindow := 0;
  133.   end
  134.   else Execute := Status;
  135. end;
  136.  
  137. function TCDFileDlg.CDExecute: Bool;
  138. begin
  139.   CDExecute := GetOpenFileName(OFN);
  140. end;
  141.  
  142. { COMMDLG requires that the hook function (ie: this method) does NOT
  143.   call EndDlg() for it's modal dialogs.  Setting Msg.Result to 0 will
  144.   allow COMMDLG to terminate the dialog.  A value of 1 will cause
  145.   COMMDLG to ignore the OK button press. }
  146. procedure   TCDFileDlg.OK(var Msg: TMessage);
  147. begin
  148.   if CanClose then
  149.     Msg.Result := 0
  150.   else
  151.     Msg.Result := 1;
  152. end;
  153.  
  154. procedure   TCDFileDlg.Cancel(var Msg: TMessage);
  155. begin
  156.   Msg.Result := 0
  157. end;
  158.  
  159.  
  160. { TCDListBox resolves a BWCC <-> CommDlg display glitch by responding
  161.   to WMEraseBkgnd messages to paint the invalidated rect using the
  162.   window background system color.  Without this, partially filled
  163.   CommDlg listboxes would be painted gray in the empty areas, leaving
  164.   the listbox half-white and half-gray.  }
  165.  
  166. type
  167.   PCDListBox = ^TCDListBox;
  168.   TCDListBox = object(TListBox)
  169.     Brush: HBrush;
  170.     constructor InitResource(AParent: PWindowsObject; ResourceID: Word);
  171.     destructor Done; virtual;
  172.     procedure WMEraseBkgnd(var Msg: TMessage);
  173.       virtual wm_First + wm_EraseBkgnd;
  174.   end;
  175.  
  176. constructor TCDListBox.InitResource(AParent: PWindowsObject;
  177.                                     ResourceID: Word);
  178. begin
  179.   inherited InitResource(AParent, ResourceID);
  180.   Brush := CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  181. end;
  182.  
  183. destructor TCDListBox.Done;
  184. begin
  185.   DeleteObject(Brush);
  186.   TListbox.Done;
  187. end;
  188.  
  189. procedure TCDListBox.WMEraseBkgnd(var Msg: TMessage);
  190. var
  191.   R: TRect;
  192. begin                          
  193.   GetClientRect(hWindow,R);
  194.   FillRect(hDC(Msg.wParam),R,Brush);
  195.   Msg.Result := 1;
  196. end;
  197.  
  198.  
  199. constructor TCDFileOpen.Init(AParent: PWindowsObject; AFlags: Longint;
  200.   AFileName: PChar; ANameLength: Word; AFilter: PChar);
  201. var
  202.   Dummy: PWindowsObject;
  203. begin
  204.   inherited Init(AParent, AFlags, AFileName, ANameLength, AFilter);
  205.   with OFN do
  206.   begin
  207.     lpstrTitle := 'File Open';
  208.     if BWCCClassNames then
  209.     begin
  210.       Flags := Flags or OFN_EnableTemplate;
  211.       lpTemplateName := dlgCDFileOpen_BWCC;
  212.       Dummy := New(PCDListBox, InitResource(@Self, 1120));
  213.       Dummy := New(PCDListBox, InitResource(@Self, 1121));
  214.     end;
  215.   end;
  216. end;
  217.  
  218. constructor TCDFileSaveAs.Init(AParent: PWindowsObject; AFlags: Longint;
  219.   AFileName: PChar; ANameLength: Word; AFilter: PChar);
  220. begin
  221.   inherited Init(AParent, AFlags, AFileName, ANameLength, AFilter);
  222.   OFN.lpstrTitle := 'File Save As';
  223. end;
  224.  
  225. function TCDFileSaveAs.CDExecute: Bool;
  226. begin
  227.   CDExecute := GetSaveFileName(OFN);
  228. end;
  229.  
  230.  
  231. end.
  232.